package com.wiscamp.ninechapters.problems.domain.services;

import com.wiscamp.ninechapters.problems.domain.models.mistakes.Mistake;
import com.wiscamp.ninechapters.problems.domain.repositories.MistakeRepository;
import com.wiscamp.ninechapters.problems.infrastructure.repositories.SheetBuilder;
import cube.ddd.aspect.HandleServiceException;
import cube.ddd.domain.BaseDomainService;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author yifeng
 * 错题本服务
 */
@Service
public class MistakeService extends BaseDomainService {

    private final MistakeRepository mistakeRepository;

    private final SheetBuilder sheetBuilder;

    private final String CACHE_NAME = "mistakes";

    public MistakeService(MistakeRepository mistakeRepository, SheetBuilder sheetBuilder) {
        this.mistakeRepository = mistakeRepository;
        this.sheetBuilder = sheetBuilder;
    }


    /**
     * 通过编号获取考试
     *
     * @param mistakeId
     * @return
     */
    @Cacheable(value = CACHE_NAME, key = "#mistakeId")
    @HandleServiceException
    public Mistake getMistake(@Min(0) long mistakeId) {
        return mistakeRepository.getMistake(mistakeId);
    }

    /*@HandleServiceException
    public Mistake getMistake(long userId, long taskId, long setId, long problemId, long questionId) {
        return mistakeRepository.getMistake(userId, taskId, setId, problemId, questionId);
    }*/


    /*@HandleServiceException
    public PagedObjects<Mistake> getMistakesByQuery(@NotNull PagedQuery query) {
        return mistakeRepository.getMistakesByQuery(query);
    }*/


    /*@HandleServiceException
    public int getMistakesQuantityByUser(@Min(0) long userId) {
        return mistakeRepository.getMistakesQuantityByUser(userId);
    }*/

    /*@HandleServiceException
    public Collection<MistakeQuantityBySetType> getGroupedMistakesQuantityBySetType(long userId) {
        return mistakeRepository.getGroupedMistakesQuantityBySetType(userId);
    }*/

    /*@HandleServiceException
    public Collection<Mistake> getMistakesBySetType(@Min(0) long userId, @Min(0) long setTypeId) {
        return mistakeRepository.getMistakesBySetType(userId, setTypeId);
    }*/


    /**
     * 创建错题本
     *
     * @param entity
     * @return
     */
    @CachePut(value = CACHE_NAME, key = "#mistake.mistakeId")
    @HandleServiceException
    public Mistake createMistake(@NotNull Mistake mistake, @Min(0) long operatorId) {
        return mistakeRepository.saveMistake(mistake);
    }

    @HandleServiceException
    public Mistake createMistake(long userId, long taskId, long setId, long problemId, long questionId) {
        var mistakeSet = new Mistake();
        mistakeSet.setProblemId(problemId);
        mistakeSet.setQuestionId(questionId);
        mistakeSet.setSetId(setId);
        mistakeSet.setTaskId(taskId);
        mistakeSet.setUserId(userId);

        var sheetDetail = sheetBuilder.getSheetDetail(taskId, problemId, questionId);
        if (sheetDetail == null) return null;
        mistakeSet.setSheetId(sheetDetail.getSheetId());
        mistakeSet.setSheetDetailId(sheetDetail.getDetailId());
        mistakeSet.setDeleted(false);
        mistakeSet.setRecordTime(LocalDateTime.now());
        return mistakeRepository.saveMistake(mistakeSet);
    }

    /**
     * 逻辑删除错题本中的错题
     *
     * @param mistakeId
     * @return
     */
    @CacheEvict(value = CACHE_NAME, key = "#mistakeId")
    @HandleServiceException
    public boolean deleteMistake(@Min(0) long mistakeId, @Min(0) long operatorId) {
        return mistakeRepository.deleteMistake(mistakeId);
    }
}
